#!/usr/bin/perl

require ("./accounts.pl");

&ReadParse();

my (%olduser, %user);
my $newpasswd = "";
my ($findflag, $uid, $error_msg, @glist, $username, $fullname);

$error_msg = "";

$username = $in{"username"};
$fullname = $in{"fullname"};

unless($username)
{
	$error_msg = $text{'error_msg_no_username'};
}

unless($error_msg)
{
	$error_msg = &check_simbol_uname($username);
	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_warning'};
	$event = $text{'accounts_naslog_event_modify_fail_uname_err_format'};
	$formatstr = sprintf $text{'accounts_naslog_modify_fail_uname_err_format'}, $username;	
}

unless($error_msg)
{
	$error_msg = &check_simbol_fullname($fullname);
	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_warning'};
	$event = $text{'accounts_naslog_event_modify_fail_fullname_err_format'};
	$formatstr = sprintf $text{'accounts_naslog_modify_fail_fullname_err_format'}, $fullname;	
}

unless($error_msg)
{
	unless($username eq $in{"oldusername"})
	{
		$uid = &local_user_exist($username);
		if($uid >= 0)
		{
			$error_msg = $text{'error_msg_user_exist_local'};
			$logminor= $text{'module_name'};
			$logpriority=$text{'naslog_warning'};
			$event = $text{'accounts_naslog_event_modify_fail_uname_exist_local'};
			$formatstr = sprintf $text{'accounts_naslog_modify_fail_uname_exist_local'}, $username;	
		}
	}
}

unless($error_msg)
{
	unless($username eq $in{"oldusername"})
	{
		$uid = &find_uid_by_name($username);
		if($uid >= 0)
		{
			$error_msg = $text{'error_msg_user_exist_sys'};
			$logminor= $text{'module_name'};
			$logpriority=$text{'naslog_warning'};
			$event = $text{'accounts_naslog_event_modify_fail_uname_exist_sys'};
			$formatstr = sprintf $text{'accounts_naslog_modify_fail_uname_exist_sys'}, $username;	
		}
	}
}

unless($error_msg)
{
	$error_msg = "";

	$user{"min"} = 0;
	$user{"shell"} = "/bin/nologin";
	$user{"real"} = $in{'fullname'};
	$user{"gid"} = &get_users_groupid();
	$user{"inactive"} = "";
	$user{"olduser"} = $in{"oldusername"};
	$user{"user"}=$in{"username"};
	$user{"home"}="/sysvol/.mail/$in{'username'}";
	if($in{"setpass"} eq "yes")
	{
		$user{"passmode"} = 3;
		$user{"plainpass"}=$in{"passwd"};
		&foreign_require("setpass", "setpass-lib.pl");
		$newpasswd = &foreign_call("setpass", "crt_cryptpasswd", $in{'passwd'});
		$user{"pass"}=$newpasswd;
	}
	if($in{"setpass"} eq "no")
	{
		$user{"passmode"} = 4;
		$user{"pass"} = &get_user_passwd($user{"olduser"});
	}

	if($in{"disabled"})
	{
		$user{"disabled"}="yes";
	}
	else
	{
		$user{"disabled"}="no";
	}

	$user{"max"}=9999;
	$user{"change"}=11915;
	$user{"warn"}="";
	$user{"expire"}="";
	$user{"uid"}= &find_uid_by_name($in{"oldusername"});

	@glist = split(/\s+/, $in{'listinput'});
	$user{"glist"} = [@glist];

	&get_user_detail(\%olduser, $in{"oldusername"});
}

if($error_msg)
{
	#	&header($text{'index_title'}, "", undef, 0, 1);
#	print "<p>", $error_msg;
	if ($event)
	{
			&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}
	&error_setup($text{'error_title_modify_user'});
	&error($error_msg);
#	&footer("/accounts/", $text{'index'});
}
else
{
	#&header($text{'index_title'}, "", undef, 0, 1);
	
	&modify_user(\%user, \%olduser);

	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_info'};
	$event = $text{'accounts_naslog_event_modify_success'};
	$formatstr = sprintf $text{'accounts_naslog_modify_success'}, $in{'oldusername'}, $username;

	&nas_syslog($logminor, $logpriority, $event, $formatstr);

	&redirect("index.cgi");
	#&footer("/accounts/", $text{'index'});
}
